$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        dataGrid: null,
        dom: {
            search: null
        },
        cloudBookPage: null,
        init: function () {
            this.cloudBookPage = xhTool.getQuery('cloudBook');
            this.dataGrid = new xhDataGrid($('#list'), 'Res/getBookList', {cloudBook: this.cloudBookPage});
            this.dataGrid.init().parseToolbar().activeButton([],['bt_list_contact', 'bt_list_contact_school', 'bt_list_use_info', 'bt_list_allow', 'bt_list_disAllow'],'eu');
            this.dom.search = $('#bt_list_search');
            $('#bt_list_referenceAllow').menubutton({
                menu: '#menu_referenceAllow'
            });
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.funObj().bindEvent();
            this.listener();
        },
        listener: function () {
            var $this = this;
            if ($this.cloudBookPage) {//省市区联动
                $('#list_search_province').combobox({
                    onSelect: function(param) {
                        var url = 'getAreaData?type=1&province='+param.value;
                        $('#list_search_city').combobox('reload', url);
                        $('#list_search_city').combobox('setValue', '');
                        $('#list_search_country').combobox('setValue', '');
                        $('#list_search_country').combobox('loadData', {"total":0,rows:[] });
                        $('#list_search_country').combobox('setValue', '');
                    }
                });
                $('#list_search_city').combobox({
                    onSelect: function(param) {
                        var url = 'getAreaData?type=2&city='+param.value;
                        $('#list_search_country').combobox('reload', url);
                        $('#list_search_country').combobox('setValue', '');
                    }
                });
                $('#list_search_country').combobox({
                    onSelect: function(param) {
                        $this.funObj().addBooInfoSelect();
                        // var url = 'getAreaData?type=2&city='+param.value;
                        // $('#list_search_country').combobox('reload', url);
                        // $('#list_search_country').combobox('setValue', '');
                    }
                });
            } else {//年级学科教材联动
                $('#list_search_grade').combobox({
                    onSelect: function(param){
                        var url = 'getSearchData?type=1&grade='+param.value;
                        $('#list_search_subject').combobox('reload', url);
                        $('#list_search_subject').combobox('setValue', '');
                        $('#list_search_book').combobox('setValue', '');
                        $('#list_search_book').combobox('loadData', {"total":0,rows:[] });
                        $('#list_search_book').combobox('setValue', '');
                    }
                });
                $('#list_search_subject').combobox({
                    onSelect: function(param){
                        var grade = xhExtra.getValue($('#list_search_grade'));
                        var url = 'getSearchData?type=2&subject='+param.value+'&grade='+grade;
                        $('#list_search_book').combobox('reload', url);
                        $('#list_search_book').combobox('setValue', '');
                    }
                });
            }
            //查询
            $this.dom.search.click(function () {
                $this.funObj().search();
            });
            xhTool.bindDocKeyDown($this.dom.search);
            //关联题库
            $('#bt_list_contact').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                window.top.xhTop.showModal({
                    title: '选择教师',
                    content: xhTool.getFrameString('Res/teacherContactBook', {
                        nav: false,
                        bookId: $this.dataGrid.getSelectValue('bookId'),
                        subject:escape($this.dataGrid.getSelectValue('subject'))
                    }, 500),
                    size: 'lg',
                    backClose:false
                });
            });
            //查看已关联学校
            $('#bt_list_contact_school').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().getConnectedSchool();
            });
            //查看使用情况
            $('#bt_list_use_info').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().getUseBookDetail();
            });
            //导出
            $('#bt_list_export').click(function () {
                $this.funObj().exportRepairTablet();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                bindEvent:function () {
                    $this.dataGrid.command({
                        view:detailview,
                        detailFormatter:function(index,row){
                            var str='<div class="ddv" style="padding:5px 0;overflow: auto;">';
                            if (row['extra'] != '') {
                                str += row['extra'];
                            }
                            str+='</div>';
                            return str;
                        },
                        onExpandRow: function(index,row){
                            var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                            ddv.panel({
                                // height:200,
                                width:780,
                                border:false,
                                cache:false,
                                onLoad:function(){
                                    $this.dataGrid.command('fixDetailRowHeight',index);
                                    $this.dataGrid.command('fixDetailRowWidth',index);
                                }
                            });
                            $this.dataGrid.command('fixDetailRowHeight',index);
                        }
                    });
                    $this.dataGrid.onLoadSuccess = function (data) {
                        $.each(data['rows'],function (index,row) {
                            if(!row['extra']){
                                $this.dataGrid.command('getExpander',index).parent().hide().after('<div style="width: 24px;"></div>');
                            }
                        })
                    }
                },
                search: function (_return) {
                    var params = {
                        status: xhExtra.getValue($('#list_search_status')),
                        search: {
                            vague: xhExtra.getValue($('#list_search_vague'))
                        },
                        // bookId: xhExtra.getValue($('#list_search_bookId')),
                        searchType: xhExtra.getValue($('#list_search_type')),
                        book: xhExtra.getValue($('#list_search_book')),
                        grade: xhExtra.getValue($('#list_search_grade')),
                        subject: xhExtra.getValue($('#list_search_subject')),
                        referApply: xhExtra.getValue($('#list_search_apply')),
                        bookNature: xhExtra.getValue($('#list_search_bookNature')),
                        cloudBook: $this.cloudBookPage
                    };
                    if (_return) {
                        params.search = xhExtra.getValue($('#list_search_vague'));
                        params.page = $('#list').datagrid("getPager").data("pagination" ).options.pageNumber;
                        params.rows = $('#list').datagrid("getPager").data("pagination" ).options.pageSize;
                        return params
                    }
                    $this.dataGrid.reload(params);
                },
                addBooInfoSelect: function () {
                    var prov = xhExtra.getValue($('#list_search_province'));
                    var city = xhExtra.getValue($('#list_search_city'));
                    var country = xhExtra.getValue($('#list_search_country'));
                    var url = 'getBookByArea?prov=' + prov + '&city=' + city + '&country=' + country;
                    $('#list_search_grade').combobox('reload', url+'&type=1').combobox('setValue', '');
                    $('#list_search_subject').combobox('reload', url+'&type=2').combobox('setValue', '');
                    $('#list_search_book').combobox('reload', url+'&type=3').combobox('setValue', '');
                },
                changeStage: function (stage) {
                    $('.stage').hide().find('select').attr('disabled', true);
                    $('.stage-' + stage).fadeIn().find('select').attr('disabled', false);
                },
                getConnectedSchool: function () {
                    window.top.xhTop.showModal({
                        title: '已关联学校',
                        content: xhTool.getFrameString('Res/showConnSchool', {bookId: $this.dataGrid.getSelectValue('bookId')}, 500),
                        size: 'md',
                        width: 800
                    });
                },
                getUseBookDetail: function () {
                    var bookNme = $this.dataGrid.getSelectValue('bookName');
                    window.top.xhTop.showModal({
                        title: bookNme + '-使用情况查询:',
                        content: xhTool.getFrameString('Res/showDetailInfo', {bookId: $this.dataGrid.getSelectValue('bookId')}, 500),
                        size: 'md',
                        width: 800
                    });
                },
                exportRepairTablet: function () {
                    location.href = baseUrl + 'Res/exportExcel?' + xhTool.parseParam($this.funObj().search(true));
                },
                allow: function (_status) {
                    var bookId = $this.dataGrid.getSelectValue('bookId');
                    var bookSecurity = $this.dataGrid.getSelectValue('bookSecurity');
                    if (_status === 1 && bookSecurity === '允许') {
                        window.top.xhTop.showMessage({
                            title: "不可操作已允许的书本",
                            text:  '',
                            type: 'error'
                        });
                        return false;
                    }
                    if (_status === 0 && bookSecurity === '不允许') {
                        window.top.xhTop.showMessage({
                            title: "不可操作已设置为不允许的书本",
                            text:  '',
                            type: 'error'
                        });
                        return false;
                    }
                    if (bookSecurity === '允许') {
                        bookSecurity = '不允许';
                    }else if (bookSecurity === '不允许') {
                        bookSecurity = '允许';
                    }
                    xhExtra.askChangeStatus('res/setBookSecurity', {bookId: bookId, bookSecurity: _status}, $this.dataGrid, '确认设置为'+bookSecurity+'？');
                }
            }
        }
    }
}

